﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class LogWriter
    {
        #region 单例
        private static readonly object Lock = new object();
        private static LogWriter _instance;
        public static LogWriter Instaince
        {
            get
            {
                if (_instance == null)
                {
                    lock (Lock)
                    {
                        if (_instance == null)
                        {
                            _instance = new LogWriter();
                        }
                    }
                }
                return _instance;
            }
        }
        #endregion
        private const string printDataTest = @"DebugLog\";
        /// <summary>
        /// 查找路径,不存在就创建
        /// </summary>
        /// <param name="StrfileName"></param>
        /// <returns></returns>
        private static string IsExistFile(string StrfileName)
        {
            lock (obj)
            {
                try
                {
                    string strTemp = printDataTest + DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + @"\";
                    if (!Directory.Exists(strTemp))
                    {
                        Directory.CreateDirectory(strTemp);
                    }
                    strTemp = strTemp + StrfileName;
                    if (!File.Exists(strTemp + ".txt"))
                    {
                        File.Create(strTemp + ".txt");
                    }
                    return strTemp + ".txt";
                }
                catch (Exception ex)
                {
                    return " ";
                }
            }
        }
        /// <summary>
        /// 日志类型记录
        /// </summary>
        /// <param name="dateName">文件名</param>
        /// <param name="data">数据内容</param>
        public void WriteLog(string dateName, string Data)
        {
            lock (obj)
            {
                try
                {
                    string time = DateTime.Now.ToString("[yy-MM-dd HH:mm:ss] ");
                    Write(dateName, time + " : " + Data);
                }
                catch (Exception ex)
                {
                    string time = DateTime.Now.ToString("[yy-MM-dd HH:mm:ss] ");
                    Write("Error", time + " : " + ex.ToString());
                }
            }
        }
        /// <summary>
        /// 写错误日志
        /// </summary>
        /// <param name="data"></param>
        public void WriteErrorLog(string Data)
        {
            lock (obj)
            {
                try
                {
                    string time = DateTime.Now.ToString("[yy-MM-dd HH:mm:ss] ");
                    Write("Error", time + " : " + Data);
                }
                catch (Exception ex)
                {
                    string time = DateTime.Now.ToString("[yy-MM-dd HH:mm:ss] ");
                    Write("Error", time + " : " + ex.ToString());
                }
            }
        }
        /// <summary>
        /// 写记录日志
        /// </summary>
        /// <param name="date"></param>
        public void WriteRecordLog(string Data)
        {
            lock (obj)
            {
                try
                {
                    string time = DateTime.Now.ToString("[yy-MM-dd HH:mm:ss]" + "\r");
                    Write("Record", time + "" + Data);
                }
                catch (Exception ex)
                {
                    string time = DateTime.Now.ToString("[yy-MM-dd HH:mm:ss]" + "\r");
                    Write("Error", time + "" + ex.ToString());
                }
            }
        }
        static object obj = new object();
        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="str"></param>
        /// <param name="logStrDate"></param>
        private void Write(string Path, string logStrData)
        {
            lock (obj)
            {
                try
                {
                    string returnPath = IsExistFile(Path);
                    FileStream file = new FileStream(returnPath, FileMode.Append);
                    file.Close();
                    StreamWriter writer = File.AppendText(returnPath);
                    writer.WriteLine(logStrData + "\r\r\r\r");
                    writer.Flush();
                    writer.Close();
                }
                catch (Exception ex)
                {
                    string time = DateTime.Now.ToString("[yy-MM-dd HH:mm:ss]" + "\r");
                    Write("Error", time + "" + ex.ToString());
                }
            }
        }
    }
}
